package mapper.Impl;

import mapper.StudentMapper;
import pojo.Student;
import utils.DBUtils;

import java.sql.SQLException;
import java.util.List;


public class StudentMapperImpl implements StudentMapper {

    @Override
    public void add(String username, String password, String name, String sex, int age, String hobbies, String photo) {
        try {
            DBUtils.commonUpdate("insert into student(username,password,name,sex,age,hobbies,photo) values(?,?,?,?,?,?,?)",username,password,name,sex,age, hobbies,photo);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(String username) {
        try {
            DBUtils.commonUpdate("delete from student where username = ?",username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(String username, String newPassword) {
        try {
            DBUtils.commonUpdate("update student set password=? where username=?",newPassword,username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(String username, String name, String sex, int age, String hobbies) {
        try {
            DBUtils.commonUpdate("update student set name=?,sex=?,age=?,hobbies=? where username=?",name,sex,age,hobbies,username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(String username, String name, String sex, int age, String hobbies, String photo) {
        try {
            DBUtils.commonUpdate("update student set name=?,sex=?,age=?,hobbies=?,photo=? where username=?",name,sex,age,hobbies,photo,username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student getStudent(String username) {

        Student student = null;
        try {
            student = DBUtils.commonQueryObj(Student.class, "select * from student where username = ?", username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return student;
    }

    @Override
    public Student getStudent(String username, String password) {
        Student student = null;
        try {
            student = DBUtils.commonQueryObj(Student.class, "select * from student where username = ? and password = ?", username, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return student;
    }

    @Override
    public List<Student> getStudents(int offset, int count) {

        List<Student> students = null;
        try {
            students = DBUtils.commonQueryList(Student.class, "select * from student limit ?,?", offset, count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return students;
    }

    @Override
    public int getAllCount() {

        int allcount = 0;
        try {
            allcount = DBUtils.getAllCount("student");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return allcount;
    }
}
